// import http from "@/utils/http"
import { setLocalHistories } from "@/utils/storage";
import store, { AppDispatch } from ".."
import { SearchType } from "../reducer/search";

/**
 * 获取联想建议（自动补全）
 */
export const getSuggestion = (q: string) => {
  return async (dispatch: AppDispatch) => {
    // const res = await http.get(`/v1_0/suggestion?q=${q}`)

    // console.log('------', res);

    setTimeout(() => {
      const res = ['aaaFU', 'bFUbb', 'ccFUcasdFU', 'FUddd']
      dispatch({ type: 'search/suggestion', payload: res })
    }, 100);

  }
}

/**
 * 点击搜索存到仓库--传递第二个参数为 true 清理仓库
 */

export const addHistory = (value: string, flag?: boolean): SearchType => {
  let newList: string[] = []
  if (!flag) {
    const { searchHistory } = store.getState().search
    const list = [value, ...searchHistory]
    newList = Array.from(new Set(list))
    if (newList.length > 10) newList.length = 10
  }
  // 存到本地
  setLocalHistories(newList)
  return { type: 'search/history', payload: newList }
}